'use strict';

require('./globals');
require('./setup-qcloud-sdk');

const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const multer = require('multer');
const Sequelize = require('sequelize');
const epilogue = require('epilogue');
const config = require('./config');

var sequelize = new Sequelize('demo', 'cdb_outerroot', 'DDLNagijkQ3x', {
    host: '10.66.181.125',
    port: 3306,
    dialect: 'mysql',
    pool: {
        max: 10,
        min: 0,
        idle: 10000
    }
});


const app = express();

sequelize
    .authenticate()
    .then(function(err) {
        console.log('Connection has been established successfully.');
        // let user = User.findOne()
        // console.log(user);
    })
    .catch(function(err) {
        console.log('Unable to connect to the database:', err);
    });



app.set('query parser', 'simple');
app.set('case sensitive routing', true);
app.set('jsonp callback name', 'callback');
app.set('strict routing', true);
app.set('trust proxy', true);

app.disable('x-powered-by');

// 记录请求日志
app.use(morgan('tiny'));

// parse `application/x-www-form-urlencoded`
app.use(bodyParser.urlencoded({ extended: true }));

// app.use(multer({ dest: "./public/images" }));

// parse `application/json`
app.use(bodyParser.json());




app.use('/', require('./routes'));



// Initialize epilogue
epilogue.initialize({
    app: app,
    sequelize: sequelize
});

// model
const Log = require("./model/log");
const User = require('./model/user');
const Message = require('./model/message');
const MessageImage = require('./model/messageImage');
const MessageLike = require('./model/messageLike');
const MessageComment = require('./model/messageComment');
const MessageReport = require('./model/messageReport');
const Notice = require('./model/notice');
const Donate = require('./model/donate');
// Create REST resource
var userResource = epilogue.resource({
    model: User,
    endpoints: ['/users', '/users/:id']
});
var messageResource = epilogue.resource({
    model: Message,
    endpoints: ['/messages', '/messages/:id']
});
var messageImageResource = epilogue.resource({
    model: MessageImage,
    endpoints: ['/message_images', '/message_images/:id']
});
var messageLikeResource = epilogue.resource({
    model: MessageLike,
    endpoints: ['/message_likes', '/message_likes/:id']
});
var messageCommentResource = epilogue.resource({
    model: MessageComment,
    endpoints: ['/message_comments', '/message_comments/:id']
});
var messageReportResource = epilogue.resource({
    model: MessageReport,
    endpoints: ['/message_reports', '/message_reports/:id']
});
var noticeResource = epilogue.resource({
    model: Notice,
    endpoints: ['/notices', '/notices/:id']
});
var donateResource = epilogue.resource({
    model: Donate,
    endpoints: ['/donates', '/donates/:id']
});


// 打印异常日志
process.on('uncaughtException', error => {
    console.log(error);
});

// 启动server
http.createServer(app).listen(config.port, () => {
    console.log('Express server listening on port: %s', config.port);
});